var sources = require('./../lib/source');
var getitem = require('./../lib/sourceitem')(sources);
module.exports = function (UC, source, config, ArFor, cache, package) {
    var UserControl = UC;
    var router = function (req, res, next) {
        var login = false;
        if (UserControl.getoff(req.sessionID, 1)) {
            login = true;
        }
        var pages = 1;
        if (req.params.pages) {
            pages = parseInt(req.params.pages);
        }
        if (req.UserAgent.isMobile) {
            res.redirect('./../../../../mobile/#Search=' + req.params.value + "/");
        } else {

            new source(getitem('W_26'), {}, function (website) {


                var manifest_id = null;

                var File = cache.GetTemplateID(package.id + '/' + website.id + '/Search/' + req.params.value + '/' + pages) + '.html';

                cache.exists(File, function (exists) {
                    //exists=false;
                    if (!exists) {
                        var category = [{
                            id: 'Home',
                            title: '首页'
                        }];
                        new source(getitem("W_10"), {
                            'website': '1'
                        }, function (db) {
                            if (db) {
                                db = db.Table;
                                db = db.reverse();
                                db.push(category[0]);
                                category = db.reverse();
                            }
                            category.push({
                                title: '搜索',
                                active: true
                            })
                            new source(getitem("W_14"), {
                                'value': req.params.value,
                                pages: pages
                            }, function (List) {
                                var size = 15;
                                var pagination = {
                                    len: 0,
                                    Next: pages + 1,
                                    Previous: pages - 1,
                                    page: pages,
                                    url: './../../../../Search/' + req.params.value + '/'
                                };
                                if (List && List.Table) {
                                      size = List.size;
                                    var total = List.total;
                                    pagination.len = total / size;
                                    List = List.Table;
                                } else {
                                    List = []
                                } 

                                cache.render({
                                    id: package.id,
                                    file: './jview/index'
                                }, {
                                    title: website.name,
                                    icon: website.icon,
                                    style: website.style,
                                    manifest_id: manifest_id,
                                    login: login,
                                    sys_id: package.id,
                                    pagination: pagination,
                                    category: category,
                                    UserAgent: req.UserAgent,
                                    PageType: 'Search',
                                    List: List,
                                    value: req.params.value
                                }, req, res, File);
                            })
                        })

                    }
                },res)
            })
        }
    };
    return router;
};